﻿using System;
using System.Collections.Generic;
using LantinxShare.LBS;
using System.Data;
using LantixLBSServer.Web.DbAccess.Helper;

namespace LantixLBSServer.Web.DbAccess
{
    public class RoleManager : IManager
    {
        public List<GRole> GetRoles()
        {
            List<GRole> list = new List<GRole>();
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                IDataReader reader = db.ExecuteDataReader("select id,RoleId,RoleName from t_role", false);
                while (reader.Read())
                {
                    GRole role = new GRole()
                    {
                        Id = reader.GetInt32(0),
                        RoleId = reader.GetInt32(1),
                        RoleName = reader.GetString(2)
                    };
                    list.Add(role);
                }
            }, () => { db.CloseConnection(); });

            return list;
        }

        public GRole GetRole(String roleName)
        {
            List<GRole> list = GetRoles();

            foreach (var item in list)
            {
                return item;
            }

            return null;
        }

        public bool UpdateRole(GRole role)
        {
            bool result = false;

            if (role == null || string.IsNullOrEmpty(role.RoleName))
                return result;

            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@RoleId");
                db.SetParameterValue("@RoleId", role.RoleId);
                db.AddParameter("@RoleName");
                db.SetParameterValue("@RoleName", role.RoleName);

                if (db.ExecuteNonQuery(@"update t_role set RoleName=@RoleName where RoleId=@RoleId ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool DeleteRole(GRole role)
        {
            bool result = false;

            if (role == null)
                return result;

            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@RoleId");
                db.SetParameterValue("@RoleId", role.RoleId);

                if (db.ExecuteNonQuery(@"delete from t_role where RoleId=@RoleId ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool InsertRole(string roleName)
        {
            bool result = false;
            if (string.IsNullOrEmpty(roleName))
                return result;

            List<GRole> list = GetRoles();
            int roleId = list.Count + 1;
            foreach (var item in list)
            {
                if (item.RoleId > roleId)
                {
                    roleId = item.RoleId + 1;
                }
            }

            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@RoleId");
                db.SetParameterValue("@RoleId", roleId);
                db.AddParameter("@RoleName");
                db.SetParameterValue("@RoleName", roleName);

                if (db.ExecuteNonQuery(@"insert into t_role(RoleId,RoleName) 
                        values(@RoleId,@RoleName)", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }
    }// end for class
}